package com.hetang.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Duo Duo on 2017/2/13.
 */
public class HttpUtils {
    /**
     * 向指定Url 发送post请求
     *
     * @param param
     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @throws Exception
     */
    public static final String sendPost(String url, String param) throws Exception {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送post请求异常！");
            throw e;
        } finally {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        }
        return result;
    }

    /**
     * 获取请求地址的所有返回数据
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static final String getHttpData(String url) throws IOException {
        // 获取openId
        URL realUrl = new URL(url);
        BufferedReader in = new BufferedReader(new InputStreamReader(realUrl.openStream()));
        String str = "";
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
        }
        return sb.toString();
    }

    /**
     * 根据key提取json数据的value
     */
    public static final String getJsonValue(String key, String json) {
        Matcher m = Pattern.compile("\"" + key + "\":\"([^\"]+)\"").matcher(json);
        String value = "";
        while (m.find()) {
            value = m.group(1);
        }
        return value;
    }

    /**
     * 根据URL地址和key获取想要的数据
     *
     * @throws IOException
     */
    public static final String getHttpJsonValue(String url, String key) throws IOException {
        // 获取openId
        URL realUrl = new URL(url);
        BufferedReader in = new BufferedReader(new InputStreamReader(realUrl.openStream()));
        String str = "";
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
        }
        Matcher m = Pattern.compile("\"" + key + "\":\"([^\"]+)\"").matcher(sb.toString());
        String value = "";
        while (m.find()) {
            value = m.group(1);
        }
        return value;
    }
}
